import { YueShu } from './YueShu.js'
import { KongZhiTi } from '../wuTi/KongZhiTi.js'

// 带快捷键操作的约束
// 加载时 生成快捷键

class JiaoLian extends KongZhiTi(YueShu) {
	shiJiaoLian = true
	suLv = 1
	yueShuFanWei = null
	
	constructor(wuTiA, wuTiB, weiZhiA, weiZhiB, canShuA, canShuB) {
		if (canShuA?.isVector3) super('铰链', { wuTiA, wuTiB, weiZhiA, weiZhiB, zuoBiaoZhouA: canShuA, zuoBiaoZhouB: canShuB }, false)
		else super('铰链', { wuTiA, wuTiB, weiZhiA, weiZhiB, xuanZhuanA: canShuA, xuanZhuanB: canShuB }, false)
		
		this.yueShuFanWei = { zuiXiao: 0, zuiDa: Math.PI * 1.999 }
	}
	
	// 默认快捷键
	moRenKuaiJieJian() {
		this.kuaiJieJian = {
			zhengXiang: {
				miaoShu: '正向',
				kuaiJieJian: 'KeyT'
			},
			fanXiang: {
				miaoShu: '反向',
				kuaiJieJian: 'KeyG'
			}
		}
	}

	jianPanXia(event) {
		let kjj = this.kuaiJieJian
		let jiShu = 0
		let fuHao = 0
	
		switch (event.code) {
			case kjj.zhengXiang.kuaiJieJian:
				fuHao = 1;
				jiShu ++
				break;
			case kjj.fanXiang.kuaiJieJian:
				fuHao = -1;
				jiShu ++
				break;
		}
		
		if (jiShu > 0) this.sheZhiYueShu({ jiaoLian: { xuanZhuanFanWei: this.yueShuFanWei, xuanZhuanSuDu: this.suLv * fuHao } })
	}

	jianPanShang(event) {
		let kjj = this.kuaiJieJian
		let jiShu = 0
		let fuHao = 0
	
		switch (event.code) {
			case kjj.zhengXiang.kuaiJieJian:
			case kjj.fanXiang.kuaiJieJian:
				fuHao = 0;
				jiShu ++
				break;
		}
		
		if (jiShu > 0) this.sheZhiYueShu({ suoDing: true, jiaoLian: { xuanZhuanSuDu: this.suLv * fuHao } })
	}
	// 处理事件
	chuLi(shiJian) {
		if (shiJian.type == 'keydown') this.jianPanXia(shiJian)
		if (shiJian.type == 'keyup') this.jianPanShang(shiJian)
	}
	
	jiaZai(fuMoXing) {
		super.jiaZai(fuMoXing)
		// zuiXiao: 0, zuiDa: Math.PI * 2 不会旋转，所以要1.999
		this.sheZhiYueShu({ suoDing: true, jiaoLian: { xuanZhuanSuDu: 0 } })
	}
	
	xieZai() {
		super.xieZai()
	}
}

export {
	JiaoLian
}